// 定义Person类
var Ball=function (userId,x, y, width, height) {
    this.userId=userId;
    this.x=x;
    this.y=y;
    this.width=width;
    this.height=height;
    this.speed=2;
    this.bullet=new Array();
    this.death=false;
    //绘制
    this.draw();
};
Ball.prototype.moveUp=function () {
    this.y-=this.speed;
};
Ball.prototype.moveDown=function () {
    this.y+=this.speed;
};
Ball.prototype.moveLeft=function () {
    this.x-=this.speed;
};
Ball.prototype.moveRight=function () {
    this.x+=this.speed;
};
Ball.prototype.draw=function(){
    context.fillStyle='olivedrab';
    context.beginPath();
    context.arc(this.x,this.y,this.width,0,2*Math.PI,false);
    context.fill();
}
Ball.prototype.beHit=function(){
    if(this.width>30&&this.height>30){
        this.width--;
        this.height--;
        this.oDiv.css({
            'width':this.width+'px',
            'height':this.height+'px'
        })
    }
}
Ball.prototype.toDeath=function(){
    var message=JSON.stringify({'state':'death','userId':this.userId});
    websocket.send(message);
}
Ball.prototype.setSize=function(width, height){
    var message=JSON.stringify({'state':'setSize','userId':this.userId,'width':width,'height':height});
    websocket.send(message);
}
Ball.prototype.hitTest=function(object, j){
    for (var i=j+1;i<object.length;i++){
        if(!this.death&&(!object[i].death)&&cirHitTest(object[i].x,object[i].y,object[i].width/2,this.x,this.y,this.width/2)){
            if(this.width>object[i].width){
                object[i].toDeath();
                var width,height;
                width=height=Math.sqrt(this.width*this.width+object[i].width*object[i].width);
                this.setSize(width,height);
                return;
            }
            if(this.width<object[i].width){
                this.toDeath();
                var width,height;
                width=height=Math.sqrt(this.width*this.width+object[i].width*object[i].width);
                object[i].setSize(width,height);
            }
        }
    }
}
Ball.prototype.exit=function () {
    document.getElementById('cqr').removeChild(this.oDiv.get(0));
}